﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movement : MonoBehaviour
{

    private Rigidbody rb;
    private Animator animator;
    private bool leftOrRight = true;
    private float turn;
    private float upforce;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.velocity = new Vector3(1.9f, 0, 0);
        animator = GetComponentInChildren<Animator>();
        animator.SetBool("isWalk", true);
        leftOrRight = true;
    }

    // Update is called once per frame
    void Update()
    {
        turn = Input.GetAxis("Horizontal");
        if(turn!=0) rb.AddTorque(transform.up * turn);
        upforce =  Input.GetAxis("Vertical");
         if(upforce!=0)rb.AddForce(transform.up * upforce);
        if (this.transform.position.x > 1.512108 && leftOrRight)
        {
            rb.velocity = new Vector3(-1.9f, 0, 0);
            this.transform.Rotate(Vector3.up * 180);
            leftOrRight = !leftOrRight;

        }
        if (this.transform.position.x < -3.012108 && !leftOrRight)
        {
            rb.velocity = new Vector3(1.9f, 0, 0);
            this.transform.Rotate(Vector3.up * 180);
            leftOrRight = !leftOrRight;
        }
    }
}
